package com.test.interview150.exam027;

import java.util.Arrays;

/**
 * https://leetcode.cn/problems/remove-element/submissions/?envType=study-plan-v2&envId=top-interview-150
 */
public class Solution01 {

    public int removeElement(int[] nums, int val) {
        int len = nums.length;
        for (int i = 0; i < len; i++) {
            if (nums[i] == val) {
                int last = nums[len - 1];
                while (last == val && len > i + 1) {
                    --len;
                    last = nums[len - 1];
                }
                --len;
                if (last == val) {
                    nums[i] = 0;
                } else {
                    nums[i] = last;
                }
            }
        }
        return len;
    }
}
